package nl.infodation.emr.pricingservice.ui;

import java.util.ArrayList;
import java.util.List;

import nl.infodation.emr.pricingservice.controller.ControllerFactory;
import nl.infodation.emr.pricingservice.data.UserSearchItem;
import nl.infodation.emr.pricingservice.entity.User;
import nl.infodation.emr.pricingservice.ui.component.ExplicitPagingContainer;
import nl.infodation.emr.pricingservice.ui.page.SessionData;
import nl.infodation.emr.utility.Constants;

import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Table;

@SuppressWarnings({ "rawtypes", "unused" })
public class UsersList extends Table {

	private static final long serialVersionUID = 1L;
	final private ExplicitPagingContainer pagingContainer = new ExplicitPagingContainer();

	private UsersManagementView parentView;

	public Object[] STANDARD_COL_NAME;

	public String[] STANDARD_COL_HEADER;

	private void initColumnData() {
		STANDARD_COL_NAME = new Object[] { "username", "business", "firstName",
				"lastName", };

		STANDARD_COL_HEADER = new String[] {
				SessionData.getI18N().getMessage(Constants.USERNAME_HEADER),
				SessionData.getI18N().getMessage(Constants.BUSINESS_HEADER),
				SessionData.getI18N().getMessage(Constants.FIRST_NAME_HEADER),
				SessionData.getI18N().getMessage(Constants.LAST_NAME_HEADER), };
	}

	public UsersList(UsersManagementView parentView) {
		this.parentView = parentView;
		initColumnData();
		setSizeFull();
		initializePagingContainer();
		setPageSize(20);
		setContainerDataSource(pagingContainer);
		setVisibleColumns(STANDARD_COL_NAME);
		setColumnHeaders(STANDARD_COL_HEADER);
		setColumnCollapsingAllowed(true);
		setColumnReorderingAllowed(true);
		setSelectable(true);
		setImmediate(true);
		setNullSelectionAllowed(false);
		addGeneratedColumn();
		selectFirstItem();
	}

	public void setParentView(UsersManagementView parentView) {
		this.parentView = parentView;
	}

	public void selectFirstItem() {
		if (pagingContainer.getItemCount() > 0) {
			select(0);
			BeanItem bean = (BeanItem) this.getItem(0);
			UsersManagementView.setCurrentUser((User) bean.getBean());
		}
	}

	private void setPageSize(int size) {
		pagingContainer.setPageSize(size);
	}

	private void addGeneratedColumn() {
		addGeneratedColumn("business", new BusinessColumnGenerator());
	}

	class BusinessColumnGenerator implements ColumnGenerator, ClickListener {

		private static final long serialVersionUID = 1L;

		public Object generateCell(Table source, Object itemId, Object columnId) {
			final BeanItem beanItem = (BeanItem) source.getItem(itemId);
			User u = (User) beanItem.getBean();
			if (null == u.getRole()) {
				return null;
			}
			return u.getRole().getRoleName();
		}

		@Override
		public void buttonClick(ClickEvent event) {

		}
	}

	private void initializePagingContainer() {
		pagingContainer.addContainerProperty("username", String.class, null);
		pagingContainer.addContainerProperty("business", String.class, null);
		pagingContainer.addContainerProperty("firstName", String.class, null);
		pagingContainer.addContainerProperty("lastName", String.class, null);

		loadPage(1);
	}

	public void loadPage(int page) {
		List<BeanItem> items = null;
		if (SessionData.isSuperAdmin()) {
			// Get all himself/users/admins.
			final UserSearchItem userCriterial = new UserSearchItem();
			userCriterial.setActive(Boolean.TRUE);
			
			items = ControllerFactory.getUserController()
					.getPaginationByCriteria(userCriterial, page,
							pagingContainer.getPageSize());

		} else if (SessionData.isNormalAdmin()) {
			// Get himself.
			UserSearchItem userCriterial = new UserSearchItem();
			userCriterial.setRoleName(SessionData.getRoleName());
			userCriterial.setActive(Boolean.TRUE);
			
			items = ControllerFactory.getUserController()
					.getPaginationByCriteria(userCriterial, page,
							pagingContainer.getPageSize());

		} else {
			items = new ArrayList<BeanItem>();
		}

		pagingContainer.setDataSource(items);
		setDefaultColumnWidth();
		
	}
	private void setDefaultColumnWidth(){
		this.setColumnWidth(STANDARD_COL_NAME[0], 150);
		this.setColumnWidth(STANDARD_COL_NAME[1], 80);
		this.setColumnWidth(STANDARD_COL_NAME[2], 230);
		this.setColumnWidth(STANDARD_COL_NAME[3], 250);
	}

	public void reload() {
		loadPage(1);
	}

	public void setDataSource(final List<BeanItem> beans) {
		pagingContainer.setDataSource(beans);
	}

	public int getPageSize() {
		return pagingContainer.getPageSize();
	}
}
